package com.xunji.user.service.impl;


import com.xunji.user.common.RedisUtil;
import com.xunji.user.service.CacheService;
import org.apache.commons.lang3.StringUtils;

public class RedisCacheService implements CacheService {

    private RedisUtil redisUtil;

    public RedisCacheService(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Override
    public <T> T get(String key, Class T) {
        if (StringUtils.isEmpty(key)) return null;
        Object o = redisUtil.get(key);
        if (null == o) return null;
        return (T) o;
    }

    @Override
    public boolean set(String key, Object val, long ttl) {
        if (StringUtils.isEmpty(key)) return false;
        redisUtil.set(key, val, ttl);
        return true;

    }

    @Override
    public boolean set(String key, Object val) {
        if (StringUtils.isEmpty(key)) return false;
        redisUtil.set(key, val);
        return true;
    }

    @Override
    public boolean remove(String key) {
        redisUtil.remove(key);
        return true;
    }

    @Override
    public boolean isExist(String key) {
        return redisUtil.isExist(key);
    }
}
